/*
 * @Author: 陈亮
 * @LastEditors: 陈亮
 * @GitHub: https://github.com/drinkwd
 * @Description: 事件管理vuex
 * @Date: 2019-02-28 14:18:29
 * @LastEditTime: 2019-03-21 14:15:22
 */

// 导入常用函数
import echarts from 'echarts'
import $http from '../utils/http.js'
import $download from '../utils/download.js'
import { getmakeStringDes } from './analyze/utils/makeString'
// 导入echarts配置
import { threatLevelopt, ReportingStatistics, disposalStatusopt } from '../components/eventdetail/js/config'
// 定义事件管理列表常量
const statistics = '/event/statistics'
const textstatistics = '/event/textstatistics'
const eventtable = '/event/eventtable'
// 定义事件管理详情常量
const eventManDetail = '/event/eventManDetail'
// 定义分析报告常量
const analysisReport = '/event/analysisReport'
const analysisdownload = '/event/analysisdownload'
// 定义mutations常量
const drawecharts = '/event/drawecharts'
const changeLoading = '/event/changeLoading'
const Muasearch = '/event/Muasearch'
const Muapage = '/event/Muapage'
const Muasize = '/event/Muasize'
const Muasort = '/event/Muasort'
const Muafilters = '/event/Muafilters'
const MuatableData = '/event/MuatableData'
const Muaappendix = '/analyze/Muaappendix'
export default {
  state: {
    loading: false,
    eventnum: 0,
    tableDate: [],
    total: 0,
    tableparams: {
      'pagination': {
        'current': 1, // 当前页码
        'pageSize': 20 // 当前页显示的数据条数
      },
      'filters': { // 需要被过滤的字段
      },
      'search': {
        'value': '' // 需要模糊搜索的值（搜索框）
      },
      'sorter': {
        order: 'descend', field: 'astart_time'
      },
      'dict': {

      }
    }
  },
  actions: {
    /**
     * @description: 事件管理统计区
     */
    [statistics] ({ commit }) {
      $http.get('/api/event/statistics').then(function ({ dataSource, status } = {}) {
        commit(changeLoading)
        if (status === 'success') {
          commit(changeLoading)
          commit(drawecharts, dataSource)
          commit(textstatistics, dataSource.total)
        }
      })
    },
    /**
     * @description: 事件管理列表区
     */
    [eventtable] ({ state, commit }, params) {
      commit(changeLoading)
      if (params.type === 'search') {
        commit(Muasearch, params.val)
      } else if (params.type === 'pagechange') {
        commit(Muapage, params.val)
      } else if (params.type === 'sizechange') {
        commit(Muasize, params.val)
      } else if (params.type === 'sort') {
        commit(Muasort, { key: params.key, value: params.value })
      } else if (params.type === 'filter') {
        commit(Muafilters, { r: params.r, v: params.v })
      }
      $http.option('/api/event/eventtable', state.tableparams).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(changeLoading)
          commit(MuatableData, data)
        }
      })
    },
    /**
     * @description: 事件管理详情
     */
    [eventManDetail] ({ state, commit }, { rowId, fun }) {
      commit(changeLoading)
      $http.post('/api/event/eventDetail', { eventId: rowId }).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(changeLoading)
          fun('eventDetail', data)
        }
      })
    },
    /**
     * @description: 分析报告
     */
    [analysisReport] ({ state, commit }, { eventId, analysisId, fun, limit }) {
      commit(changeLoading)
      $http.post('/api/event/analyzeReport', { eventId: eventId, analysisId: analysisId, limit: limit }).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(changeLoading)
          commit(Muaappendix, { data: data.appendix })
          fun('report', data)
        }
      })
    },
    /**
     * @description: 下载分析依据
     */
    [analysisdownload] ({ commit }, { path }) {
      $download.post('/api/event/download', { path: path })
    }
  },
  mutations: {
    /**
     * @description: 绘制统计图
     */
    [drawecharts] (state, dataSource) {
      let ThreatLevel = echarts.init(document.getElementById('threatLevel'))
      let attackTrTrend = echarts.init(document.getElementById('attackTrTrend'))
      let disposalStatus = echarts.init(document.getElementById('disposalStatus'))
      let threatLeveloptX = []
      let threatLeveloptSeries = []
      for (let key in dataSource.level) {
        let leveldict = {}
        leveldict.value = dataSource.level[key]
        switch (key) {
          case 'level1':
            leveldict.name = `低危：${dataSource.level[key]}`
            break
          case 'level2':
            leveldict.name = `中危：${dataSource.level[key]}`
            break
          case 'level3':
            leveldict.name = `高危：${dataSource.level[key]}`
            break
          case 'level4':
            leveldict.name = `严重：${dataSource.level[key]}`
            break
        }
        threatLeveloptX.push(leveldict.name)
        threatLeveloptSeries.push(leveldict)
      }
      threatLevelopt.legend.data = threatLeveloptX
      threatLevelopt.series.data = threatLeveloptSeries
      let ReportX = []
      let ReportSeries = []
      for (let key of dataSource.waves) {
        ReportX.push(key.r_time)
        ReportSeries.push(key.event_count)
      }
      ReportingStatistics.xAxis.data = ReportX
      ReportingStatistics.series.data = ReportSeries
      let disposalStatusoptX = []
      let disposalStatusoptSeries = []
      for (let key in dataSource.status) {
        let disposal = {}
        disposal.value = dataSource.status[key]
        switch (key) {
          case 'process':
            disposal.name = `修复中：${dataSource.status[key]}`
            break
          case 'completed':
            disposal.name = `已修复：${dataSource.status[key]}`
            break
          case 'undone':
            disposal.name = `未修复：${dataSource.status[key]}`
            break
          case 'defense':
            disposal.name = `成功防御：${dataSource.status[key]}`
            break
        }
        disposalStatusoptX.push(disposal.name)
        disposalStatusoptSeries.push(disposal)
      }
      disposalStatusopt.legend.data = disposalStatusoptX
      disposalStatusopt.series.data = disposalStatusoptSeries
      disposalStatus.setOption(disposalStatusopt)
      attackTrTrend.setOption(ReportingStatistics)
      ThreatLevel.setOption(threatLevelopt)
    },
    /**
     * @description: 统计区数据
     */
    [textstatistics] (state, data) {
      state.eventnum = data
    },
    /**
     * @description: 加载页面loading
     */
    [changeLoading] (state) {
      state.loading = !state.loading
    },
    /**
     * @description: 事件管理列表分页
     */
    [Muapage] (state, currentpage) {
      state.tableparams.pagination.current = currentpage
    },
    /**
     * @description: 事件管理列表更改size
     */
    [Muasize] (state, pagesize) {
      state.tableparams.pagination.pageSize = pagesize
      state.tableparams.pagination.current = 1
    },
    /**
     * @description: 事件管理列表搜索
     */
    [Muasearch] (state, query) {
      state.tableparams.search.value = query
      state.tableparams.pagination.current = 1
    },
    /**
     * @description: 事件管理列表排序
     */
    [Muasort] (state, { key, value }) {
      state.tableparams.sorter.order = value
      state.tableparams.sorter.field = 'a' + key
      state.tableparams.pagination.current = 1
    },
    /**
     * @description: 事件管理列表过滤
     */
    [Muafilters] (state, { r, v }) {
      if (r === 'arisk_level') {
        state.tableparams.filters.arisk_level = v
      }
      if (r === 'ahandle_status') {
        state.tableparams.filters.ahandle_status = v
      }
      state.tableparams.pagination.current = 1
    },
    /**
     * @description: 表格赋值
     */
    [MuatableData] (state, data) {
      state.tableDate = data.dataSource
      state.total = data.total
    },
    /**
     * @description: 分析报告附录
     */
    [Muaappendix] (state, { data }) {
      for (let index in data) {
        let log = data[index]
        log.describes = getmakeStringDes(log)
        log.log_type = getmakeStringDes(log, 'name')
      }
      // state.checkedLogData.data = data.dataSource
      // state.checkedLogData.total = data.total
    }
  }
}
